﻿using System;
using System.Net;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using DataObjectsPhone;
using System.Collections.ObjectModel;
using SharedAgentLib;
using System.ComponentModel;

namespace ee_wp_tourmaker.DataControllers
{
    public class MutexData 
    {
        public String ProcessName = "taquest";
        public String BackGroundPNG = "background.png";
        public bool CycleAgentEveryMinute { get; set; }


        private String flag;
        public String Flag { get { if (flag == null)flag = ""; return flag; } set { flag = value; } }

        private IsoStorageData _mutexeData;

         public IsoStorageData MutexedData
        {
            get
            {
                return _mutexeData;
            }
            set
            {
                if (value != _mutexeData)
                {
                    _mutexeData = value;
                }
            }
        }

        public MutexData()
        {
            _mutexeData = new IsoStorageData();
            CycleAgentEveryMinute = false;

        }

        public void ReadData()
        {
            // read mutexed iso data
            MutexedData = MutexedIsoStorageFile.Read();

            List<CheckPoint> readCheckPoints = MutexedData.checkPoints;

            if (readCheckPoints != null)
            {
                foreach (CheckPoint p in readCheckPoints)
                {
                    if (p.Done)
                    {
                        Flag = "CheckPoint";
                        App.MainModel.SelectedCheckPoint = p;
                    }
                }
                
                App.MainModel.SelectedCheckPoint = readCheckPoints[0];
                App.MainModel.NotifiyPropertyChanged("checkpoint");
            }
        }

        public void WriteMutex()
        {
            WriteintoisoStorage(collectUnDoneCheckPoints());
        }

        private void WriteintoisoStorage( List<CheckPoint> points)
        {
            //construct data
            MutexedData.LastTimeFileTouched = DateTime.Now;
            MutexedData.LastProcessToTouchFile = ProcessName;
            MutexedData.CycleAgentEveryMinute = CycleAgentEveryMinute;
            MutexedData.checkPoints = points;

            //write mutexed iso data
            MutexedIsoStorageFile.Write(MutexedData);
        }

        private List<CheckPoint> collectUnDoneCheckPoints()
        {
            List<Challenge> array = App.ChallengeData.ActiveChallenges;
            List<CheckPoint> checkpoints = new List<CheckPoint>();
            try
            {
                for (int i = 0; i < array.Count; i++)
                {
                    ObservableCollection<CheckPoint> points = array[i].checkpointsList;
                    if (points != null)
                    {
                        for (int j = 0; j < points.Count; j++)
                        {
                            if (points[i].Done == false)
                            {
                                checkpoints.Add(points[j]);
                            }
                        }
                    }
                }

            }
            catch (Exception)
            {
               
            }

            

            return checkpoints;

        }





    }
}
